import { Component } from '@angular/core';
import { ApesField } from './apes-field';
import { ApiService } from '@core/services/http/api.service';
import { Events } from '@core/services/events/Events';
import { ExpressionService } from '@core/services/expression/ExpressionService';
import { SubjectService } from '@core/services/events/subject.service';
import { dateFormat } from '../../../../utils/utils';

@Component({
  selector: 'formly-field-time',
  template: `
    <apes-time-picker style="width: 100%;"
                      [apesFormat]="formatType"
                      [apesDisabled]="to.disabled"
                      [formControl]="formControl"
                      [formlyAttributes]="field"
                      [apesPlaceHolder]="to.placeholder"
                      (apesOpenChange)="change($event)">
    </apes-time-picker>
  `,
  host: {},
})
export class FormlyFieldTime extends ApesField {
  // HH:mm:ss
  formatType: string = 'HH:mm:ss';

  constructor(protected apiService: ApiService, protected events: Events, protected expressionService: ExpressionService, protected subject: SubjectService) {
    super(apiService, events, expressionService, subject);
  }



  ngOnInit() {
    this.addDefalutPlaceholder('请选择');
    if (this.to['valueFormatter']) {
      this.formatType  = this.to['valueFormatter'];
    }
  }
  change(params){
    if (params){
      if(this.formatType == 'HH:mm' || this.formatType == 'HH:mm:ss'){
        if (this.formControl.value){
          this.formControl.patchValue(new Date(this.formControl.value));
        }
       }else {
        this.formControl.patchValue(new Date());
      }
    }
  }

}
